#include <iostream>
#include <cmath>
#include <stdlib.h>
#include <time.h>
#include "main.h"


#include "WorldStatusUpdate.pb.h"
#include "Commands.pb.h"
#include "ProtobufUtil.hh"

using namespace strategy_protocol;
using namespace protoutil;
using namespace std;

void TheStrategy::go_n_block()
{
//	int blocker_x[] = {-6, 0, 6, -6, 6, -6,  0,  6, -3, 3, -6, 6, -6, 6, -3,  3};
//	int blocker_y[] = { 6, 6, 6,  0, 0, -6, -6, -6,  6, 6, -3, 3, -3, 3, -6, -6};
	int blocker_x[] = {-3, 0, -2, -3, 3, 3, 0, 3, 2, -3, -1, 3, -3, 1, -2, -1};
	int blocker_y[] = {3,  3, -1,  0, 0,-1,-3,-3, 1,  1, -2,-2,  2, 2,  3,  3};

    for(unsigned int i=0;i<blockers_size;i++)
    {
        Position expected_pos; //boss_pos+(blocker_x[i],blocker_y[j])
        expected_pos.set_x(boss_pos.x()+blocker_x[i]);
        expected_pos.set_y(boss_pos.y()+blocker_y[i]);
        soldiers_target_pos.push_back(expected_pos);
    }
//	cerr << "go_n_block ....." << endl;
//	bool all_alive = true;
//	for(unsigned int i=0;i<blockers_size && i<invaders.size();i++)
//	{
//		bool alive = false;
//		for(int j=0;j<world.soldiers_size() && !alive;j++)
//		{
//			if(invaders[i]==world.soldiers(j).common().id())
//			{
//				alive = true;
//				Position expected_pos; //boss_pos+(blocker_x[i],blocker_y[j])
//				expected_pos.set_x(boss_pos.x()+blocker_x[i]);
//				expected_pos.set_y(boss_pos.y()+blocker_y[i]);
//
//				soldiers_task[world.soldiers(j).common().id()].task=MOVE;
//				soldiers_task[world.soldiers(j).common().id()].target_pos=expected_pos;
//				soldiers_task[world.soldiers(j).common().id()].approx=true;
//			}
//		}
//		if(!alive)
//		{
//			dead_blocker = i;
//			all_alive = false;
//		}
//	}
//	if(all_alive)
//		dead_blocker = -1;
//	if(boss_found)
//		for(unsigned int i=blockers_size;i<invaders.size();i++)
//			for(int j=0;j<world.soldiers_size();j++)
//				if(invaders[i]==world.soldiers(j).common().id())
//				{
//					soldiers_task[world.soldiers(j).common().id()].task=MOVE;
//					soldiers_task[world.soldiers(j).common().id()].target_pos=boss_pos;
//					soldiers_task[world.soldiers(j).common().id()].approx=true;
//				}
}

void TheStrategy::go_n_kill_boss()
{
//    vector<Position> free_spaces = exists_free_space(boss_pos);
//	bool free=(free_spaces.size()!=0);
//	unsigned int free_index=0;
//	for(int j=0;j<world.soldiers_size();j++)
//	{
//		if(free)
//            if(distance(world.soldiers(j).common().position(),boss_pos) <= soldier_shoot_dist)
//            {
//                soldiers_task[world.soldiers(j).common().id()].task=ATTACK;
//                soldiers_task[world.soldiers(j).common().id()].target_pos=boss_pos;
//                attackcmd(world.soldiers(j),boss_id);
//            }
//            else
//            {
//                soldiers_task[world.soldiers(j).common().id()].task=MOVE;
//                if(free_index<free_spaces.size())
//                {
//                    soldiers_task[world.soldiers(j).common().id()].target_pos=free_spaces[free_index];
//                    soldiers_task[world.soldiers(j).common().id()].approx=false;
//                    free_index++;
//                }
//                else
//                {
//                    soldiers_task[world.soldiers(j).common().id()].target_pos=boss_pos;
//                    soldiers_task[world.soldiers(j).common().id()].approx=true;
//                }
//            }
//		else
//		{
//			soldiers_task[world.soldiers(j).common().id()].task=MOVE;
//			soldiers_task[world.soldiers(j).common().id()].target_pos=boss_pos;
//			soldiers_task[world.soldiers(j).common().id()].approx=true;
//		}
//
//	}

}

void TheStrategy::attack_nearby_minions()
{
	vector<int> opp_soldiers;
	for(unsigned int j = 0;j < world.soldiers_size();++j)
		if(world.soldiers(j).team() != MYTEAM)
			opp_soldiers.push_back(j);

	vector<int> opp_workers;
	for(unsigned int j = 0;j < world.workers_size();++j)
		if(world.workers(j).team() != MYTEAM)
			opp_workers.push_back(j);
	//ellenséges katonákat csak katonával támadjuk
	for(unsigned int i=0;i<opp_soldiers.size();i++)
	{
		int current_opp_soldier_health = world.soldiers(opp_soldiers[i]).health();
		for(unsigned int j = 0; current_opp_soldier_health>0 && j < world.soldiers_size();++j)
			if(world.soldiers(j).team() == MYTEAM && !soldier_has_target[j])
				if(distance(world.soldiers(j).common().position(),world.soldiers(opp_soldiers[i]).common().position()) <= soldier_shoot_dist)
				{
					soldier_has_target[j] = true;
					current_opp_soldier_health -= soldier_strength;
					attackcmd(world.soldiers(j),world.soldiers(opp_soldiers[i]).common().id());
				}

	}
	//ellenséges parasztot mindennel támadunk
	for(unsigned int i=0;i<opp_workers.size();i++)
	{
		int current_opp_worker_health = world.workers(opp_workers[i]).health();
		for(unsigned int j = 0; current_opp_worker_health>0 && j < world.soldiers_size();++j)
			if(world.soldiers(j).team() == MYTEAM && !soldier_has_target[j])
				if(distance(world.soldiers(j).common().position(),world.workers(opp_workers[i]).common().position()) <= soldier_shoot_dist)
				{
					soldier_has_target[j] = true;
					current_opp_worker_health -= soldier_strength;
					attackcmd(world.soldiers(j),world.workers(opp_workers[i]).common().id());
				}
		for(unsigned int j = 0; current_opp_worker_health>0 && j < world.workers_size();++j)
			if(world.workers(j).team() == MYTEAM && !worker_has_target[j])
				if(distance(world.workers(j).common().position(),world.workers(opp_workers[i]).common().position()) <= worker_shoot_dist)
				{
					worker_has_target[j] = true;
					current_opp_worker_health -= worker_strength;
					attackcmd(world.workers(j),world.workers(opp_workers[i]).common().id());
				}
	}
	for(unsigned int i=0;i<world.bases_size();++i)
		if(world.bases(i).team() != MYTEAM)
		{
			for(unsigned int j = 0;j < world.soldiers_size();++j)
				if(world.soldiers(j).team() == MYTEAM && !soldier_has_target[j])
					if(distance(world.soldiers(j).common().position(),world.bases(i).common().position()) <= soldier_shoot_dist)
					{
						soldier_has_target[j] = true;
						attackcmd(world.soldiers(j),world.bases(i).common().id());
					}
			for(unsigned int j = 0; j < world.workers_size();++j)
				if(world.workers(j).team() == MYTEAM)
					if(distance(world.workers(j).common().position(),world.bases(i).common().position()) <= worker_shoot_dist)
					{
						worker_has_target[j] = true;
						current_opp_worker_health -= worker_strength;
						attackcmd(world.workers(j),world.bases(i).common().id());
					}
		}
}

void TheStrategy::go_n_attack_boss(int j)
{
//	if(boss_found && !soldier_has_target[j])
//	{
//		if(distance(world.soldiers(j).common().position(),boss_pos) <= soldier_shoot_dist)
//		{
//			soldier_has_target[j] = true;
//			attackcmd(world.soldiers(j), boss_id);
//		}
//		else
//		{
//			Position new_pos = world.soldiers(j).common().position();
//			movecmd(world.soldiers(j), new_pos);
//		}
//	}
}

void TheStrategy::everybody_go_n_attack_boss()
{
//	if(boss_found)
//		for(int j = 0; j < world.soldiers_size();++j)
//			go_n_attack_boss(j);
}
